/*
 * @Author: xzy 1453077527@qq.com
 * @Date: 2025-08-24 00:34:58
 * @LastEditors: xzy 1453077527@qq.com
 * @LastEditTime: 2025-08-26 17:01:58
 * @Description:
 */
package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.QuestionTypeDao;
import com.xtedu.examproject.entity.QuestionType;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class QuestionTypeDaoImpl implements QuestionTypeDao {
    @Override
    public int insert(QuestionType questionType) throws Exception {
        String sql = "insert into questiontype(type_name) values(?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, questionType.getName());
            return ps.executeUpdate();
        }
    }

    @Override
    public int update(QuestionType questionType) throws Exception {
        String sql = "update questiontype set type_name=? where id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, questionType.getName());
            ps.setInt(2, questionType.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int delete(QuestionType questionType) throws Exception {
        String sql = "delete from questiontype where id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, questionType.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public QuestionType findById(int id) throws Exception {
        String sql = "select * from questiontype where id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            QuestionType questionType = new QuestionType();
            if (rs.next()) {
                questionType.setId(rs.getInt("type_id"));
                questionType.setName(rs.getString("type_name"));
            }
            return questionType;
        }
    }

    @Override
    public List<QuestionType> findAll() throws Exception {
        String sql = "select * from questiontype";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            List<QuestionType> questionTypes = new ArrayList<QuestionType>();
            while (rs.next()) {
                QuestionType questionType = new QuestionType();
                questionType.setId(rs.getInt("id"));
                questionType.setName(rs.getString("type_name"));
                questionTypes.add(questionType);
            }
            return questionTypes;
        }
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }
}
